package micro_demo

import (
	"context"
	"protobuf/pb"
	"time"

	"go-micro.dev/v4"
	"go-micro.dev/v4/util/log"
)

// send events using the publisher
func sendEv(topic string, p micro.Publisher) {
	timer := time.NewTicker(1 * time.Second)

	for range timer.C {
		// create new event
		ev := &pb.BarResponse{
			Instrument: "rb2210." + topic,
			Time:       []string{time.Now().Format("15:04:05")},
		}

		// publish an event
		if err := p.Publish(context.Background(), ev); err != nil {
			log.Logf("error publishing %v", err)
		}
		log.Logf("publishing %+v\n", ev)
	}
	log.Logf("send finish")
}

func Publish() {
	// create a service
	service := micro.NewService(
		micro.Name("future"),
		micro.Version("latest"),
	)

	// initialise flags
	service.Init()

	// create publisher
	pub1 := micro.NewEvent("future.1m", service.Client())

	// pub to topic 1
	go sendEv("future.1m", pub1)

	// pub to topic 2
	pub2 := micro.NewEvent("future.5m", service.Client())
	go sendEv("future.5m", pub2)

	// block forever
	select {}
}
